/**
 * 
 */
package com.efast.dmom.aps.datahandlers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.aps.bean.RouteStep;
import com.efast.dmom.aps.bean.WorkCenter;
import com.efast.dmom.aps.dao.RouteStepWCRelDao;
import com.efast.dmom.aps.entity.RouteStepWcEntity;
import com.efast.dmom.aps.entity.base.IModificationFlags;
import com.efast.dmom.aps.util.TimeUtil;

/**
 * @author Aaron Liu
 *
 *         2020年2月13日
 */
@Component
public class RouteStepWCRelDataHandler {

	@Autowired
	private RouteStepWCRelDao routeStepWCRelDao;

	protected void saveRelation(long routeKey, List<RouteStep> stepList) {

		if (stepList == null) {
			return;
		}

		List<RouteStepWcEntity> newRouteWcRelList = new ArrayList<RouteStepWcEntity>();
		List<RouteStepWcEntity> removedRouteWcRelList = new ArrayList<RouteStepWcEntity>();
		List<RouteStepWcEntity> modifiedRouteWcRelList = new ArrayList<RouteStepWcEntity>();

		for (RouteStep step : stepList) {

			List<WorkCenter> wcList = step.getWorkCenterList();
			
			if(wcList != null){
				for (WorkCenter wc : wcList) {
					
					int modificationFlag = wc.getInnerData().getModificationFlag();
					
					if(IModificationFlags.DELETED == modificationFlag){
						
						RouteStepWcEntity relEntity = new RouteStepWcEntity();
						
						relEntity.setStepUuid(step.getInnerData().getUuid());
						relEntity.setWcKey(wc.getInnerData().getId());
						
						removedRouteWcRelList.add(relEntity);
					}
					
					if(IModificationFlags.MODIFIED == modificationFlag){
						
						RouteStepWcEntity relEntity = new RouteStepWcEntity();
						
						relEntity.setStepUuid(step.getInnerData().getUuid());
						relEntity.setWcKey(wc.getInnerData().getId());
						relEntity.setOpKey(step.getInnerData().getOperationKey());
						relEntity.setRouteKey(routeKey);
						relEntity.setStepUuid(step.getInnerData().getUuid());
						relEntity.setRouteStepKey(step.getInnerData().getId());
						relEntity.setWcKey(wc.getInnerData().getId());
						relEntity.setCapacity(wc.getInnerData().getCapacity());
						relEntity.setpLineKey(wc.getInnerData().getLineId());
						relEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
						relEntity.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
						relEntity.setLastModifier(SpringWebUtils.getRequestUser());
						
						modifiedRouteWcRelList.add(relEntity);
					}
					
					if(IModificationFlags.NEW == modificationFlag){
						
						RouteStepWcEntity relEntity = new RouteStepWcEntity();

						relEntity.setOpKey(step.getInnerData().getOperationKey());
						relEntity.setRouteKey(routeKey);
						relEntity.setStepUuid(step.getInnerData().getUuid());
						relEntity.setRouteStepKey(step.getInnerData().getId());
						relEntity.setWcKey(wc.getInnerData().getId());
						relEntity.setCapacity(wc.getInnerData().getCapacity());
						relEntity.setpLineKey(wc.getInnerData().getLineId());
						relEntity.setSiteId(DataHandlerUtils.getSiteId());
						relEntity.setCreationTime(TimeUtil.getLocalDateTime());
						relEntity.setCreationTimeZone(TimeUtil.getLocalTimeZone());
						relEntity.setCreator(SpringWebUtils.getRequestUser());
						relEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
						relEntity.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
						relEntity.setLastModifier(SpringWebUtils.getRequestUser());
						
						newRouteWcRelList.add(relEntity);
					}

				}
			}
		}
		
		if(removedRouteWcRelList.size() > 0){
			routeStepWCRelDao.removeRelationList(removedRouteWcRelList);
		}
		
		if(newRouteWcRelList.size() > 0){
			routeStepWCRelDao.saveRelation(newRouteWcRelList);
		}
		
		if(modifiedRouteWcRelList.size() > 0){
			routeStepWCRelDao.updateRelation(modifiedRouteWcRelList);
		}
		
	}

	protected void removeRelation(List<RouteStep> stepList) {

		if (stepList == null || stepList.size() <=0) {
			return;
		}

		List<RouteStepWcEntity> relEntityList = new ArrayList<RouteStepWcEntity>();

		for (RouteStep step : stepList) {
			
			List<WorkCenter> wcList = step.getWorkCenterList();
			
			for(WorkCenter wc : wcList){
				
				RouteStepWcEntity relEntity = new RouteStepWcEntity();
				
				relEntity.setStepUuid(step.getInnerData().getUuid());
				relEntity.setWcKey(wc.getInnerData().getId());
				
				relEntityList.add(relEntity);
			}
		}
		
		if(relEntityList.size() > 0){
			routeStepWCRelDao.removeRelationList(relEntityList);
		}
	}

	//verify that the operation is referenced by the route_wc_rel
	public int getRouteStepWcRelByOperationId(long operationId) {
		return routeStepWCRelDao.getRouteStepWcRelByOperationId(operationId);
	}

	public double getProdLineCapacity(long routeKey, long pLineKey) {
		return routeStepWCRelDao.getProdLineCapacity(routeKey,pLineKey);
	}
}
